package com.controller.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class loginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        String  name = (String) req.getSession().getAttribute("LOGIN_NAME");
        if (name == null) {
            //未登录情况下
            if (req.getRequestURL().toString().contains("/css") || req.getRequestURL().toString().contains("/js")
                    || req.getRequestURL().toString().contains("/images") || req.getRequestURL().toString().contains("/amazeui")
                    || req.getServletPath().equals("/login.jsp") || req.getServletPath().equals("/user")|| req.getServletPath().equals("/randomCode")
                    || req.getServletPath().equals("/regist.jsp") || req.getServletPath().equals("/findPwd.jsp")){
                filterChain.doFilter(servletRequest, servletResponse);
            }else {
                //未登录则
                //重定向到登录界面
                resp.sendRedirect(req.getContextPath() + "/login.jsp");
            }
        }else {
            //登录则无法重复登录
            if (!req.getServletPath().equals("/login.jsp")) {
                filterChain.doFilter(servletRequest, servletResponse);
            }else {
                resp.sendRedirect(req.getContextPath() + "/emp/index");
            }
        }
    }
}
